package com.hake.core.util;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.ui.freemarker.FreeMarkerTemplateUtils;
import org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer;

import freemarker.core.ParseException;
import freemarker.template.Configuration;
import freemarker.template.MalformedTemplateNameException;
import freemarker.template.Template;
import freemarker.template.TemplateException;
import freemarker.template.TemplateNotFoundException;

@Component
public class FreeMarkerKit {

	
	@Autowired
	private FreeMarkerConfigurer freemarker ;
	
	/**
	 * 模板默认存放目录
	 */
	private static final String DEFAULT_PATH = "/WEB-INF/views/template/" ;
	
	/*public FreeMarkerKit() {
		configuration = new Configuration(Configuration.VERSION_2_3_23);
	}*/
	
	/**
	 * 根据模板生成HTML
	 * @param dataModel 数据模型
	 * @param templateFolder 模板存在目录，如：/template或/WEB-INF/views/template
	 * @param templateName 模板文件名称
	 * @param out HTML输出路径，如：c:/test.html
	 */
	public void generator(Map<String, Object> dataModel, String templateFolder, String templateName, String out) {
		FileOutputStream fos = null ;
		OutputStreamWriter osw = null ;
		BufferedWriter bw = null ;
		try {
			String templateFullPath = DEFAULT_PATH ;
			if(StringUtils.isNotBlank(templateFolder)) {
				templateFullPath = (templateFolder+=templateFolder.lastIndexOf("/")==0?"/":"") + templateName ;
			} else {
				templateFullPath += templateName ;
			}
			
			Template template = getTemplate(templateFullPath.replace("//", "/")) ;
			
			fos= new FileOutputStream(new File(out));
			osw =new OutputStreamWriter(fos, "UTF-8");
			bw =new BufferedWriter(osw, 1024);
			
			template.process(dataModel, bw);
			
		} catch (TemplateException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			try {
				if(null != bw) {
					bw.flush();
					bw.close();
				}
				if(null != osw) {
					osw.flush(); 
					osw.close();
				}
				if(null != fos) {
					fos.flush();
					fos.close();
				}
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}
	/**
	 * 根据模板生成HTML，使用项目默认的模板存放目录 /WEB-INF/views/template/
	 * @param dataModel 数据模型
	 * @param templateName 模板文件名称
	 * @param out HTML输出路径，如：c:/test.html
	 */
	public void generator(Map<String, Object> dataModel, String templateName, String out) {
		generator(dataModel, null, templateName, out);
	}
	
	/**
	 * 返回模板生成后的字符串
	 * @param dataModel 数据模型
	 * @param templateName 模板名称
	 * @return 生成后的HTML字符串
	 */
	public String generator(Map<String, Object> dataModel, String templateName) {
		try {
			return FreeMarkerTemplateUtils.processTemplateIntoString(getTemplate(DEFAULT_PATH+templateName), dataModel);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
	
	
	/**
	 * 根据模板生成HTML，用于junit调试
	 * @param dataModel 数据模型
	 * @param templateFolder 模板存在目录，如：src/main/webapp/template
	 * @param templateName 模板文件名称
	 * @param out HTML输出路径，如：c:/test.html
	 */
	public void generatorDebug(Map<String, Object> dataModel, String templateFolder, String templateName, String out) {
		FileOutputStream fos = null ;
		OutputStreamWriter osw = null ;
		BufferedWriter bw = null ;
		try {
			this.freemarker.getConfiguration().setDirectoryForTemplateLoading(new File(templateFolder));
			Template template = getTemplate(templateName) ;
			
			fos= new FileOutputStream(new File(out));
			osw =new OutputStreamWriter(fos, "UTF-8");
			bw =new BufferedWriter(osw, 1024);
			
			template.process(dataModel, bw);
			
		} catch (TemplateException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			try {
				if(null != bw) {
					bw.flush();
					bw.close();
				}
				if(null != osw) {
					osw.flush(); 
					osw.close();
				}
				if(null != fos) {
					fos.flush();
					fos.close();
				}
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}
	
	/**
	 * 用于junit调试，不设置ServletContext
	 * @param dataModel 数据模型
	 * @param templateFolder 模板存放目录路径，如：src/main/webapp/template
	 * @param templateName 模板名称
	 */
	public void writeConsoleDebug(Map<String, Object> dataModel, String templateFolder, String templateName) {
		try {
			this.freemarker.getConfiguration().setDirectoryForTemplateLoading(new File(templateFolder));
			Template template = getTemplate(templateName) ;
			template.process(dataModel, new PrintWriter(System.out));
		} catch (TemplateNotFoundException e) {
			e.printStackTrace();
		} catch (MalformedTemplateNameException e) {
			e.printStackTrace();
		} catch (ParseException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} catch (TemplateException e) {
			e.printStackTrace();
		}
	}
	
	/**
	 * 输出到控制台，用于WEB调试
	 * @param dataModel 数据模型
	 * @param templateName 模板名称
	 */
	public void writeConsoleDebug(Map<String, Object> dataModel, String templateName) {
		try {
			Template template = getTemplate(templateName) ;
			template.process(dataModel, new PrintWriter(System.out));
		} catch (TemplateNotFoundException e) {
			e.printStackTrace();
		} catch (MalformedTemplateNameException e) {
			e.printStackTrace();
		} catch (ParseException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} catch (TemplateException e) {
			e.printStackTrace();
		}
	}
	
	public Template getTemplate(String templateName) {
		try {
			return this.freemarker.getConfiguration().getTemplate(templateName) ;
		} catch (TemplateNotFoundException e) {
			e.printStackTrace();
		} catch (MalformedTemplateNameException e) {
			e.printStackTrace();
		} catch (ParseException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return null;
	}
	

	public Configuration getConfiguration() {
		return freemarker.getConfiguration();
	}

}

